<!-- 编辑弹窗 -->
<template>
  <ele-modal
    inner
    fullscreen
    :visible="visible"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="'采购单列表'"
    @update:visible="updateVisible" :before-close="handleClose"
  >
    <div class="ele-body">
      <ProductchooseSearch @search="reload"  />
      <el-card>
      <!-- 搜索表单 -->
      <el-row :gutter="24">
        <el-col  :md="12" :sm="24">
          <ele-pro-table ref="table"  row-key="id"  sub-title="未选采购单:" :toolkit="[]"  :columns="allorderColumns"  :datasource="datasource"   @done="onDone" :need-page="true"  :height="tableHeight" :full-height="tableFullHeight">
            <template v-slot:toolkit>
              <el-button size="mini" class="ele-btn-icon" @click="addall">
                全部添加
              </el-button>
            </template>
            <template v-slot:action="{ row }">
              <el-button size="mini" @click="add(row)">添加</el-button>
            </template>
          </ele-pro-table>
        </el-col>
        <el-col  :md="12" :sm="24">
          <ele-pro-table ref="choosetable"  row-key="id"   sub-title="已选采购单:" :toolkit="[]"   :columns="chooseOrderColumns"  :datasource="choosePurOrderDatas"   :need-page="false"  :height="tableHeight" :full-height="tableFullHeight">
            <template v-slot:toolkit>
              <el-button size="mini" class="ele-btn-icon" @click="removeAll" type="danger">
                全部删除
              </el-button>
            </template>
            <template v-slot:action="{ row }">
              <el-button size="mini" type="danger" @click="remove(row)">删除</el-button>
            </template>
          </ele-pro-table>
        </el-col>
      </el-row>
      
      </el-card>
    </div>
    <div slot="footer">
      <el-button type="primary" :loading="loading" @click="save" >
        确定
      </el-button>
    </div>
  </ele-modal>
</template>

<script>
 
  import { ChoosePurOrder } from '@/api/inventory/inorder';
  import { QueryProductPageList } from '@/api/basicdatamanager/productmanager';
  import ProductchooseSearch from './productChoose-Search.vue';

 
  export default {
    name: 'productchooseList',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    components: {ProductchooseSearch},
    data() {
      return {
        fixedHeight:true,
        // 表单数据
        form: {},
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        //表格选中数据(多选)，如果要回显选中数据可在 done 事件里面使用 setSelectedRowKeys 方法
        choosePurOrderDatas:[],
        // 表格列配置
        allorderColumns: [
          { columnKey: 'action',label: '操作',width: 80,align: 'center',slot: 'action'},
          {prop: 'code', label: '产品编号', showOverflowTooltip: true, sortable: 'code',align: 'center',},
          {prop: 'name', label: '产品名称', showOverflowTooltip: true, sortable: 'name',align: 'center',},
          {prop: 'spc', label: '规格', showOverflowTooltip: true,sortable: 'spc',align: 'center',},
          {prop: 'colorDesc', label: '颜色', width: 80,showOverflowTooltip: true,sortable: 'colorId',align: 'center',},
          {prop: 'unitDesc', label: '单位', width: 80,showOverflowTooltip: true, sortable: 'unitId',align: 'center',},
        ],
        // 表格列配置
        chooseOrderColumns: [
          {columnKey: 'action',label: '操作',width: 80,align: 'center',slot: 'action'},
          {prop: 'code', label: '产品编号', showOverflowTooltip: true, sortable: 'code',align: 'center',},
          {prop: 'name', label: '产品名称', showOverflowTooltip: true, sortable: 'name',align: 'center',},
          {prop: 'spc', label: '规格', showOverflowTooltip: true,sortable: 'spc',align: 'center',},
          {prop: 'colorDesc', label: '颜色', width: 80,showOverflowTooltip: true,sortable: 'colorId',align: 'center',},
          {prop: 'unitDesc', label: '单位', width: 80,showOverflowTooltip: true, sortable: 'unitId',align: 'center',},
        ],
      };
    },

    computed: {
      // 表格高度
      tableHeight() {
        var fh = this.fixedHeight ? 'calc(100vh - 380px)' : void 0;
        return fh;
      },
      // 表格全屏时高度
      tableFullHeight() {
        var fh =  this.fixedHeight ? 'calc(100vh - 280px)' : null;
        return fh;
      },
    },
   created(){
   },
    methods: {
      
      /* 表格数据源 */
      async datasource(params) {
        
        params.where.pageindex = params.page;
        params.where.pagesize = params.limit;

        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }

        params.where.pageindex = params.page;
        params.where.pagesize = params.limit;

        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }

        if(!params.where.conditions) params.where.conditions = [];
        params.where.conditions.push({ key:"status",operator: 0,value: 0,})
        params.where.conditions.push({ key:"deleted",operator: 0,value: 0,})

        let data = await QueryProductPageList(params.where);

        this.$nextTick(() => {
          this.$refs.table.doLayout();
        });

        return {
            list:data.items,
            count:data.totalrecords,
        };
      },

      save(){

        if(this.choosePurOrderDatas.length==0){
          this.$message({
            message: '没有选择产品',
            type: 'warning'
          });
        }else{

          this.$emit('callback',this.choosePurOrderDatas );
          this.$emit('update:visible', false);
        }
      },

      /* 表格渲染完成回调 */
      onDone({ data }) {

      },
      /* 刷新表格 */
      reload(where) {
        if(this.$refs.table){
          this.$refs.table.reload({ where: where });
        }
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
        this.choosePurOrderDatas=[];
      },

      handleClose() {
        //更新父组件中的visible的值
        this.$emit('update:visible', false);
        this.choosePurOrderDatas=[];
      },

      /* 全部添加 */
      addall(){

        let params = {
          pageindex: 1,
          pagesize: 0,
          conditions: [],
          orderBys: [{ sort: "createDate", order: 1, }],
        };

        params.conditions.push({ key:"status",operator: 0,value: 0,})
        params.conditions.push({ key:"deleted",operator: 0,value: 0,})

        QueryProductPageList(params).then((res)=>{
          this.choosePurOrderDatas = res.items;
        });
      },
      
      /* 添加 */
      add(row) {

        let p = this.choosePurOrderDatas.find((item)=>{
          return row.code == item.code 
        });

        if(p){
         p = row; 
        }
        else{
          this.choosePurOrderDatas.push(row);
        }
      },
      /* 移除 */
      remove(row) {
        this.choosePurOrderDatas.splice(this.choosePurOrderDatas.indexOf(row), 1);
      },
      /* 移除所有 */
      removeAll() {
        this.choosePurOrderDatas.splice(0, this.choosePurOrderDatas.length);
      }

    },
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        this.choosePurOrderDatas=[];
        if(this.$refs.table){
            this.reload();
          }
      }
    }
  };
</script>
<style scoped>

  
  </style>